﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PlugNT.Custom;
using PlugNT.Cms.Page;
using PlugNT.Cms.Model;
using PlugNT.Cms.BLL;

namespace PlugNT.Cms.Web.Admin.Pre_Comment
{
    public partial class Reply : AdminPage
    {
        private Comment curComment = new Comment();
        private string currpostfix = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            currpostfix = (Request.QueryString["currpostfix"] == null) ? WebConfig.TablePostfix : Request.QueryString["currpostfix"].Trim();
            curComment.CurPostfix = currpostfix;
            if (!IsPostBack)
            {
                string id = Request.QueryString["id"] == null ? "" : Request.QueryString["id"].Trim();
                if (id != "")
                {
                    InitData(int.Parse(id));
                }
            }
        }

        private void InitData(int id)
        {
            CommentInfo model = curComment.GetCommentInfo(id);
            if (model != null)
            {
                hidId.Value = id.ToString();
                litTitle.Text = "<div style='color:#ccc;'>" + model.title + "</div>";
                litContent.Text = model.content;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string content = txtReply.Text.Trim();
            string errorMessage = "";
            bool flag = false;
            string username = "";
            if (content != "")
            {
                if (hidId.Value.Trim() != "")
                {
                    CommentInfo model = curComment.GetCommentInfo(int.Parse(hidId.Value.Trim()));
                    if (model != null)
                    {
                        if (model.layer != 0)
                        {
                            int layer = model.layer + 1;

                            string tContent = curComment.ReUpdateContent(true, model.content, content, base.adminer.name, layer.ToString());

                            model.content = tContent;
                            model.layer = 0;
                            username = model.username;
                            flag = curComment.UpdateContent(model);
                        }
                        else
                        {
                            errorMessage = "您已回复过，回复失败！&z=no";
                        }
                    }
                }
                else
                {
                    errorMessage = "ID为空，回复失败！&z=no";
                }
            }
            else
            {
                errorMessage = "内容为空，回复失败！&z=no";
            }
            if (flag)
            {
                Response.Redirect("/Admin/Message.aspx?message=回复用户--<b>[" + username + "]</b>--成功！&z=yes");
            }
            else
            {
                Response.Redirect("/Admin/Message.aspx?message=" + errorMessage);
            }
        }
    }
}
